package cn.chl.service.impl;

import cn.chl.domain.Device;
import cn.chl.domain.Floor;
import cn.chl.dto.GetDeviceDto;
import cn.chl.mapper.DeviceMapper;
import cn.chl.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl implements IDeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 添加设备
     */
    @Override
    public void add(Device device) {
        if (device.getType() == null){
            throw new RuntimeException("设备类型不能为空！");
        }
        if (device.getName() == null){
            throw new RuntimeException("设备名不能为空！");
        }
        if (device.getBuildId() == null){
            throw new RuntimeException("楼栋不能为空！");
        }
        if (device.getFloorId() == null){
            throw new RuntimeException("楼层不能为空！");
        }

        deviceMapper.add(device);
    }

    /**
     * 查询设备信息
     */
    @Override
    public GetDeviceDto getDeviceInfoById(Integer id) {

        // 查询出device
        Device device = deviceMapper.getDeviceInfoById(id);
        // 创建getDevice，作为返回给前端的数据
        GetDeviceDto getDeviceDto = new GetDeviceDto();

        if (device.getType() == 1){
            getDeviceDto.setType("烟感");
        }else if (device.getType() == 2){
            getDeviceDto.setType("手报");
        }else if (device.getType() == 3){
            getDeviceDto.setType("摄像头");
        }
        getDeviceDto.setName(device.getName());
        getDeviceDto.setBuildId(device.getBuildId());
        getDeviceDto.setFloorId(device.getFloorId());
        return getDeviceDto;
    }

    /**
     * 通过楼宇id查询设备信息
     */
    @Override
    public List<Device> getBuildById(Integer id) {
        return deviceMapper.getBuildById(id);
    }
    /**
     * 通过楼层id查询设备信息
     */
    @Override
    public List<Device> getFloorById(Integer id) {
        return deviceMapper.getFloorById(id);
    }
    /**
     * 通过设备类型查询设备信息
     */
    @Override
    public List<Device> getTypeById(Integer type) {
        return deviceMapper.getTypeById(type);
    }
}
